package org.extremengine.swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.KeyEvent;
import java.util.Calendar;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.extremengine.swing.LongPressKeyListener;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * 
 * @author yuxing
 *
 */
public class LongPressKeyListenerSandbox extends JFrame {

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LongPressKeyListenerSandbox frame = new LongPressKeyListenerSandbox();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LongPressKeyListenerSandbox() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(final MouseEvent e) {
				handleContentPaneMouseClicked(e);
			}
		});
		contentPane.addKeyListener(new LongPressKeyListener() {
			@Override
			public void keyDown(final KeyEvent e) {
				handleContentPaneKeyPressed(e);
			}

			@Override
			public void keyUp(final KeyEvent e) {
				handleContentPaneKeyReleased(e);
			}
		});
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
	}

	protected void handleContentPaneKeyPressed(final KeyEvent e) {
		System.out.printf("KeyDown  %d  T:%d \r\n", Calendar.getInstance().getTimeInMillis(), e.getKeyCode());
	}

	protected void handleContentPaneKeyReleased(final KeyEvent e) {
		System.out.printf("KeyUp %d  T:%d \r\n", Calendar.getInstance().getTimeInMillis(), e.getKeyCode());
		System.out.println();
	}

	protected void handleContentPaneMouseClicked(final MouseEvent e) {
		contentPane.grabFocus();
		contentPane.setBackground(Color.DARK_GRAY);
	}
}
